import { createStore } from 'vuex';

export default createStore({
  state: {
    user: {
      username: '张三',
      favorites: []
    },
    restaurants: [
      // 示例数据
      { id: 1, name: '美味轩', image: 'path/to/image.jpg', rating: 4.5, menu: [{name: '宫保鸡丁', price: 38, image: 'path/to/dish.jpg'}] },
      // 更多餐厅...
    ]
  },
  mutations: {
    addToFavorites(state, restaurantId) {
      const restaurant = state.restaurants.find(r => r.id === restaurantId);
      if (restaurant && !state.user.favorites.includes(restaurant)) {
        state.user.favorites.push(restaurant);
      }
    }
  },
  actions: {
    addFavorite({ commit }, restaurantId) {
      commit('addToFavorites', restaurantId);
    }
  },
  getters: {
    getRestaurantById: (state) => (id) => {
      return state.restaurants.find(restaurant => restaurant.id === id);
    }
  }
});